#include <crt_endian.h>

int _Is_BigLittle_Endian(void)
{
	int val = 0x04030201;
	char firstByte = *(char*)&val;

	if (firstByte == 0x01) {
		return Little_endian;
	} else if (firstByte == 0x04) {
		return Big_endian;
	}
	return Big_endian;
}



unsigned short BLEndianUshort(unsigned short value)
{
	return ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8);
}

unsigned int BLEndianUint32(unsigned int value)
{
	return ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8) | ((value & 0x00FF0000) >> 8) | ((value & 0xFF000000) >> 24);
}


float BLEndianFloat(float value)
{
	typedef union FLOAT_CONV
	{
		float f;
		char c[4];
	}float_conv;

	float_conv d1, d2;
	d1.f = value;
	d2.c[0] = d1.c[3];
	d2.c[1] = d1.c[2];
	d2.c[2] = d1.c[1];
	d2.c[3] = d1.c[0];
	return d2.f;
}


unsigned short _read16_le(const unsigned char *buf)
{
	return (unsigned short)(((unsigned short)buf[0]) |
		((unsigned short)buf[1] << 8));
}

unsigned int _read32_le(const unsigned char *buf)
{
	return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
		((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
}

void _write32_le(const int value, unsigned char *buffer)
{
	buffer[0] = (char)(value & 0x000000FF);
	buffer[1] = (char)((value & 0x0000FF00) >> 8);
	buffer[2] = (char)((value & 0x00FF0000) >> 16);
	buffer[3] = (char)((value & 0xFF000000) >> 24);
}

void _write64_le(const int64_t value, unsigned char *buffer)
{
	_write32_le((int)value, buffer);
	_write32_le((int)(value >> 32), buffer + 4);
}

